<?php

class Message_AdminController extends Ac_AdminController
{

    public function dashboardAction()
    {
    	if(!empty($_GET['from']) && !empty($_GET['to']))
		{
			$from = new MongoDate (strtotime($_GET['from']." 00:00:01"));
			$to = new MongoDate (strtotime($_GET['to']." 23:59:59"));
        	$this->view->num_active_message = Model_Message::all(array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER, 'c_dt' => array('$gte' => $from, '$lte' => $to), 'ls' => array('$exists' => false)))->count();
        	$this->view->num_users = Model_Message::distinct('fo', array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER, 'c_dt' => array('$gte' => $from, '$lte' => $to)));
        }else
        {
			$this->view->num_active_message = Model_Message::all(array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER))->count();
			$this->view->num_users = Model_Message::distinct('fo', array('tp' => Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER));
		}
    }

    public function menuAction()
    {
        $this->view->index = $this->_getParam('index');
    }

    public function indexAction() 
    {
        $this->listAction();
    }
    
    public function listAction() 
    {
        $per_page = 20;
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Message::all(array('ls' => array('$exists' => false)))->count();
        $this->view->page = $page;

        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }

        if (!empty($_GET['message'])) {
            $term = new MongoRegex('/.*'.$_GET['message'].'.*/');
            if ($page>1) {
                $this->view->list           = Model_Message::all( array( 'fo' => array('$ne' => 1), 'ls' => array('$exists' => false),  '$or' => array( array('su' => $term) , array('ct' => $term) , array('fo' => $term) , array('to' => $term) ) ) )->skip($skip_page)->limit($per_page)->sort(array('c_dt' => -1));
            }
            else {
                $this->view->list           = Model_Message::all( array('fo' => array('$ne' => 1), 'ls' => array('$exists' => false),   '$or' => array( array('su' => $term) , array('ct' => $term) , array('fo' => $term) , array('to' => $term) ) ) )->limit($per_page)->sort(array('c_dt' => -1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        else {
            if ($page>1) {
                $this->view->list           = Model_Message::all(array('fo' => array('$ne' => 1), 'ls' => array('$exists' => false)))->skip(($_GET['page']-1) * $per_page)->limit($per_page)->sort(array('c_dt' => -1));
            }
            else {
                $this->view->list           = Model_Message::all(array('fo' => array('$ne' => 1), 'ls' => array('$exists' => false) ))->limit($per_page)->sort(array('c_dt' => -1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        $this->render('list');
    }

    public function displayAction()
    {
        $message = Model_Message::find($_GET['id']);

        $this->view->message = $message;
        $this->render('display');
    }
    
    public function sendAction()
	{
		$msg = Model_Message::one(array('_id' => new MongoId($_POST['id'])));
		//send pending message to posters
		$subject = $msg->su;
		$message_sender = $msg->message_txt;
		$email_poster = $msg->to;
		$listing = Model_Listing::one(array('_id' => $msg->ls));
		$email_sender = $msg->fo['email'];
		$phone = "";
		
		$msg->st = Model_Message::STATUS_SENT;
		$msg->save();
		
		Ac_Util::sendEmail($subject, $message_sender, $email_poster, true, array('listing' => $listing, 'email_sender' => $email_sender, 'phone_number' => $phone), Ac_Mail::TEMPLATE_SENDMESSAGE_POSTER, array('email' => $email_sender, 'name' => 'AsiaClassified'));
	
		$this->spamAction();
		
	}
	
	public function deleteAction()
	{
		$msg = Model_Message::remove(array('_id' => new MongoId($_POST['id'])));	
		$this->spamAction();	
	}
	
	public function spamAction() 
    {
        $per_page = 20;
        $page      = !empty($_GET['page']) ? $_GET['page'] : 1;
        $this->view->per_page = $per_page;
        $this->view->listing_number = Model_Message::all(array('st' => Model_Message::STATUS_SPAM))->count();
        $this->view->page = $page;

        if ($page>1) {
            $skip_page = ($_GET['page']-1) * $per_page;
        }

        if (!empty($_GET['message'])) {
            $term = new MongoRegex('/.*'.$_GET['message'].'.*/');
            if ($page>1) {
                $this->view->list           = Model_Message::all( array('st' => Model_Message::STATUS_SPAM) )->skip($skip_page)->limit($per_page)->sort(array('c_dt' => -1));
            }
            else {
                $this->view->list           = Model_Message::all( array('st' => Model_Message::STATUS_SPAM) )->limit($per_page)->sort(array('c_dt' => -1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        else {
            if ($page>1) {
                $this->view->list           = Model_Message::all(array('st' => Model_Message::STATUS_SPAM))->skip(($_GET['page']-1) * $per_page)->limit($per_page)->sort(array('c_dt' => -1));
            }
            else {
                $this->view->list           = Model_Message::all(array('st' => Model_Message::STATUS_SPAM))->limit($per_page)->sort(array('c_dt' => -1));
            }
            $this->view->listing_number = $this->view->list->count();
        }
        $this->render('spam-list');
    }
}
